home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 24 aspnet applications / aspnetapplications / global.asax.vb < prev    next >
Encoding:
Text File  |  2002-03-18  |  10.4 KB  |  265 lines

  1. Imports System.Web
  2. Imports System.Web.SessionState
  3.  
  4. ' set the following constant to True to enable persistent custom Sessions
  5. ' based on XML files. Read comments below for additional steps you
  6. ' may need to enable xml sessions.
  7. #Const XML_SESSIONS = False
  8.  
  9. ' set the following constant to True to enable a custom error page
  10. #Const CUSTOM_ERROR_PAGE = False
  11.  
  12. ' set the following constant to True to enable a global output filter
  13. ' that converts all <B> tags into <STRONG>
  14. #Const GLOBAL_FILTER = False
  15.  
  16. Public Class Global
  17.     Inherits System.Web.HttpApplication
  18.  
  19. #Region " Component Designer Generated Code "
  20.  
  21.     Public Sub New()
  22.         MyBase.New()
  23.  
  24.         'This call is required by the Component Designer.
  25.         InitializeComponent()
  26.  
  27.         'Add any initialization after the InitializeComponent() call
  28.  
  29.     End Sub
  30.  
  31.     'Required by the Component Designer
  32.     Private components As System.ComponentModel.IContainer
  33.  
  34.     'NOTE: The following procedure is required by the Component Designer
  35.     'It can be modified using the Component Designer.
  36.     'Do not modify it using the code editor.
  37.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  38.         components = New System.ComponentModel.Container()
  39.     End Sub
  40.  
  41. #End Region
  42.  
  43.     ' these are globally shared variables
  44.     Public Shared StartedTime As Date = Date.Now
  45.     Public Shared Counter As Integer
  46.  
  47.     ' this event fires when the application starts
  48.  
  49.     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
  50.         Diagnostics.Debug.WriteLine("Application_Start")
  51.         ' Cache the XML file when the application start.
  52.         CacheEmployeesData()
  53.     End Sub
  54.  
  55.     ' this event fires when the application ends
  56.  
  57.     Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
  58.         Diagnostics.Debug.WriteLine("Application_End")
  59.     End Sub
  60.  
  61.     ' Another way to specify the Start event (commented out)
  62.  
  63.     'Sub Application_OnStart(ByVal sender As Object, ByVal e As EventArgs)
  64.     '    Diagnostics.Debug.WriteLine("Application_OnStart")
  65.     'End Sub
  66.  
  67.     ' this event fires when the session is started
  68.  
  69.     Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
  70.         Diagnostics.Debug.WriteLine("Session_Start")
  71.     End Sub
  72.  
  73.     ' this event fires when the session ends
  74.  
  75.     Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
  76.         Diagnostics.Debug.WriteLine("Session_End")
  77.     End Sub
  78.  
  79.     ' Alternative ways to specify session events (commented)
  80.  
  81.     'Sub Session_OnStart(ByVal sender As Object, ByVal e As EventArgs)
  82.     '    Diagnostics.Debug.WriteLine("Session_OnStart")
  83.     'End Sub
  84.  
  85.     ' this event fires after ASP.NET has created the Session collection
  86.  
  87.     Private Sub Global_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.BeginRequest
  88.         Diagnostics.Debug.WriteLine("Global_BeginRequest")
  89.     End Sub
  90.  
  91. #If XML_SESSIONS Then
  92.     ' IMPORTANT: create a directory on the following path, or change
  93.     ' the path to match an existing directory
  94.     Const SESSIONDATAPATH = "C:\SessionData\"
  95.  
  96.     Private Sub Global_AcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AcquireRequestState
  97.         Diagnostics.Debug.WriteLine("Global_AcquireRequestState")
  98.  
  99.         Dim fs As System.IO.FileStream
  100.         Dim sf As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
  101.  
  102.         Try
  103.             ' Get the special cookie, exit if not found.
  104.             Dim cookie As HttpCookie = Request.Cookies("PermSessionID")
  105.             If (cookie Is Nothing) Then
  106.                 ' If not found, generate it now (using pseudo-random SessionID).
  107.                 cookie = New HttpCookie("PermSessionID", Session.SessionID)
  108.                 ' Send it to the client browser, with one-week expiration
  109.                 cookie.Expires = Now.AddDays(7)
  110.                 ' Uncomment next line to see behavior with much shorter expiration.
  111.                 'cookie.Expires = Now.AddMinutes(1)
  112.                 Response.Cookies.Add(cookie)
  113.                 ' nothing else to do for now
  114.                 Exit Try
  115.             End If
  116.  
  117.             ' The filename is equal to the value of this cookie.
  118.             Dim permSessionId As String = cookie.Value
  119.             ' Build the name of the data file.
  120.             Dim filename As String = SESSIONDATAPATH & permSessionId.ToString & ".xml"
  121.  
  122.             ' Open the file, exit if error
  123.             fs = New System.IO.FileStream(filename, IO.FileMode.Open)
  124.             ' Deserialize the hashtable that contains values.
  125.             Dim ht As Hashtable = DirectCast(sf.Deserialize(fs), Hashtable)
  126.  
  127.             ' Move data into the Session collection.
  128.             Dim key As String
  129.             Session.Clear()
  130.             For Each key In ht.Keys
  131.                 Session(key) = ht(key)
  132.             Next
  133.         Catch ex As Exception
  134.             ' ignore any exceptions.
  135.         Finally
  136.             ' Close the stream on exit.
  137.             If Not (fs Is Nothing) Then fs.Close()
  138.         End Try
  139.     End Sub
  140.  
  141.     ' this event fires after ASP.NET is about to save the Session collection
  142.  
  143.     Private Sub Global_ReleaseRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ReleaseRequestState
  144.         Diagnostics.Debug.WriteLine("Global_ReleaseRequestState")
  145.  
  146.         ' Get the special cookie.
  147.         Dim cookie As HttpCookie = Request.Cookies("PermSessionID")
  148.         ' The value of the cookie is the name of the .ses file.
  149.         Dim permSessionID As String = cookie.Value
  150.  
  151.         ' Move data from the Session collection into a Hashtable.
  152.         Dim ht As New Hashtable(Session.Count)
  153.         Dim key As String
  154.         For Each key In Session.Keys
  155.             ht(key) = Session(key)
  156.         Next
  157.         ' Clear the regular session collection, to save memory.
  158.         Session.Clear()
  159.  
  160.         Dim fs As System.IO.FileStream
  161.         Try
  162.             ' Build the name of the data file.
  163.             Dim filename As String = SESSIONDATAPATH & permSessionID.ToString & ".xml"
  164.             ' Open the file for outout, exit if error
  165.             fs = New System.IO.FileStream(filename, IO.FileMode.Create)
  166.             ' Serialize the hashtable that contains values.
  167.             Dim sf As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
  168.             sf.Serialize(fs, ht)
  169.         Catch ex As Exception
  170.             ' ignore any exceptions.
  171.         Finally
  172.             ' Close the stream on exit.
  173.             If Not (fs Is Nothing) Then fs.Close()
  174.         End Try
  175.     End Sub
  176. #End If
  177.  
  178.     ' other application events, more or less in the order they fire
  179.  
  180.     Private Sub Global_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AuthenticateRequest
  181.         Diagnostics.Debug.WriteLine("Global_AuthenticateRequest")
  182.     End Sub
  183.  
  184.     Private Sub Global_AuthorizeRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AuthorizeRequest
  185.         Diagnostics.Debug.WriteLine("Global_AuthorizeRequest")
  186.     End Sub
  187.  
  188.     Private Sub Global_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Disposed
  189.         Diagnostics.Debug.WriteLine("Global_Disposed")
  190.     End Sub
  191.  
  192.     Private Sub Global_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.EndRequest
  193.         Diagnostics.Debug.WriteLine("Global_EndRequest")
  194.     End Sub
  195.  
  196.     ' this event fires when an unhandled event occurs
  197.  
  198.     Private Sub Global_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error
  199.         Diagnostics.Debug.WriteLine("Global_Error")
  200.  
  201. #If CUSTOM_ERROR_PAGE Then
  202.         ' Prepare an error report.
  203.         Response.Clear()
  204.         Response.Write("<H1>An exception has occurred:</H1>")
  205.         ' Display information on the page being processed.
  206.         Response.Write("<b>URL = </b>" & Request.Path & "<br />")
  207.         Response.Write("<b>QueryString = </b>" & Request.QueryString.ToString & "<p>")
  208.         Response.Write("<b>Error details</b><p>")
  209.  
  210.         ' Get a reference to the (real) error that occurred.
  211.         Dim ex As Exception = Server.GetLastError.InnerException
  212.         ' Convert the string in a format that is suitable for HTML output
  213.         Dim errMsg As String = Server.HtmlEncode(ex.ToString)
  214.         errMsg = errMsg.Replace(ControlChars.CrLf, "<BR />")
  215.  
  216.         Response.Write(errMsg)
  217.         Response.End()
  218. #End If
  219.  
  220.     End Sub
  221.  
  222.     Private Sub Global_PostRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PostRequestHandlerExecute
  223.         Diagnostics.Debug.WriteLine("Global_PostRequestHandlerExecute")
  224.     End Sub
  225.  
  226.     ' this event fires just before the page executes 
  227.  
  228.     Private Sub Global_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRequestHandlerExecute
  229.         Diagnostics.Debug.WriteLine("Global_PreRequestHandlerExecute")
  230.  
  231. #If GLOBAL_FILTER Then
  232.         ' if we want to filter the page output we assign a filter object
  233.         ' to the Response.Filter property
  234.         ' NOTE: for simplicity, this program relies on the garbage collection
  235.         ' for disposing the ConvertTagFilter. In a more robust implementation
  236.         ' you should destroy it orderly after the HTML has been sent to the client
  237.         Response.Filter = New ConvertTagFilter(Response.Filter)
  238. #End If
  239.     End Sub
  240.  
  241.     ' other application events
  242.  
  243.     Private Sub Global_PreSendRequestContent(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreSendRequestContent
  244.         Diagnostics.Debug.WriteLine("Global_PreSendRequestContent")
  245.     End Sub
  246.  
  247.     Private Sub Global_PreSendRequestHeaders(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreSendRequestHeaders
  248.         Diagnostics.Debug.WriteLine("Global_PreSendRequestHeaders")
  249.     End Sub
  250.  
  251.     Private Sub Global_ResolveRequestCache(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ResolveRequestCache
  252.         Diagnostics.Debug.WriteLine("Global_ResolveRequestCache")
  253.     End Sub
  254.  
  255.     Private Sub Global_UpdateRequestCache(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.UpdateRequestCache
  256.         Diagnostics.Debug.WriteLine("Global_UpdateRequestCache")
  257.     End Sub
  258. End Class
  259.  
  260. ' this module contains a shared variable that is visible from 
  261. ' any other page of the application
  262.  
  263. Module Globals
  264.     Public Counter As Integer
  265. End Module